package com.monkey.demo.service.impl;

import com.monkey.bean.PageResponse;
import com.monkey.demo.convert.SysUserConvert;
import com.monkey.demo.dao.SysUserDao;
import com.monkey.demo.entity.SysUser;
import com.monkey.demo.entity.req.SysUserReq;
import com.monkey.demo.service.SysUserService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SysUser)表服务实现类
 *
 * @author makejava
 * @since 2023-11-25 22:24:01
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserDao sysUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @Cacheable(cacheNames = "groupName_SysUser",key ="'queryById'+#id")
    public SysUser queryById(Long id) {
        return this.sysUserDao.queryById(id);
    }

    /**
     * 分页查询
     * @return 查询结果
     */
    @Override
    public PageResponse<SysUser> queryByPage(SysUserReq sysUserReq) {
        SysUser sysUser = SysUserConvert.INSTANCE.sysUserReqConvertSysUser(sysUserReq);
        PageResponse<SysUser> response = new PageResponse<>();
        response.setCurrentPage(sysUserReq.getCurrentPage());
        response.setPageSize(sysUserReq.getPageSize());
        long total = this.sysUserDao.count(sysUser);
        List<SysUser> sysUserList = this.sysUserDao.queryAllByLimit(sysUser, (sysUserReq.getCurrentPage() - 1) * sysUserReq.getPageSize(), sysUserReq.getPageSize());
        response.setRecords(sysUserList);
        response.setTotal(total);
        return response;
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser insert(SysUser sysUser) {
        this.sysUserDao.insert(sysUser);
        return sysUser;
    }

    /**
     * 修改数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser update(SysUser sysUser) {
        this.sysUserDao.update(sysUser);
        return this.queryById(sysUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.sysUserDao.deleteById(id) > 0;
    }

    @Override
    public List<SysUser> selectList() {
        return sysUserDao.selectList();
    }
}
